import { defineStore } from 'pinia'
 import homeApi from '@/LC/api/home';
 import socket from './socket.js';
 	
 const useTickerStore = defineStore({
  id: 'ticker', // id必填，且需要唯一
  state: () => {
    return {
      ticker:{
		  
	  },
	  tickerlist:[],
	  market:[],
	  nowCode:'12350',
	  nowId:1,
	  zhibiao:{},
	  kline:{},
	  tardid:{},
	  zhishu:{},
	  nowdetails:{},
		Trade:{},
		buy:{},
		sell:{} 
    }
  },
  unistorage: {
  	// 初始化恢复前触发
  	beforeRestore(ctx) {},
  
  
  },
   actions: {
     setTiker(a){
		 this.ticker[a.key]=a.obj
	 },
	 async getcuerryList(){
		 let res= await homeApi.stock()
		 if(res.code===200){
		 	
		 	this.tickerlist=res.data.list
		 	
		 }
	 },
	 setNow(code){
		
		 
		 if(this.nowCode===code){
			 return
		 }
		 let socketStore=socket()
		 
		 this.nowCode=code
		 socketStore.sendMsg({code:this.nowCode}) 
		 // this.getNodetails()
	 },
	 setKline(data,key,date){
		if(!this.kline[key]){
			this.kline[key]={}
		}
		this.kline[key][date]=data 
	 },
	setTrade(data,key){
		
		if(!this.Trade[key]){
			this.Trade[key]=[]
		}
		for(let j=0;j< data.length;j++ ){
			
			if(this.tardid[data[j].tradeId] ){
				continue
			}
			
			if(data[j].direction==='sell'){
				if(!this.sell[key]){
					this.sell[key]=[]
				}
			
				this.sell[key].unshift(data[j])
				if(this.sell[key].length>10){
					const del=this.sell[key].pop()
					delete this.tardid[del.tradeId]
				}
			}else{
				if(!this.buy[key]){
					
					this.buy[key]=[]
				}
			
				this.buy[key].unshift(data[j])
				if(this.buy[key].length>10){
					
					const del=this.buy[key].pop()
					delete this.tardid[del.tradeId]
				}
			}
			if(!this.Trade[key]){
				this.Trade[key]=[]
			}
			this.Trade[key].unshift(data[j])
			
			this.tardid[data[j].tradeId]=true
			if(this.Trade[key].length>30){
				this.Trade[key].pop()
			}
			
			
		}
		
		
	},
	setZhibiao(arr){
		
		for(let k in arr){
			
			this.zhibiao[k]=arr[k]
		}
		
	},
	setzhishu(arr){
		
		for(let k in arr){
			
			this.zhishu[arr[k].id]=arr[k]
		}
		
	},
	 async getNodetails(){
		
		
		let a=await  homeApi.marketid(this.nowId)
		if(a.code===200){
			
			this.nowdetails=a.data
		}
	 },
	 
    },
})
 export default useTickerStore;